# encoding: utf-8

import os.path
import shutil
import re

# 遍历指定目录，显示目录下的所有文件名
def eachFile(filepath):
    pathDir = os.listdir(filepath)
    for allDir in pathDir:
        # child = os.path.join('%s\%s' % (filepath, allDir))
        child = os.path.join(filepath, allDir)
        if os.path.isfile(child):
            readFile(child) 
            continue
        eachFile(child)

# 遍历出结果 返回文件的名字
def readFile(filenames):
    f = open(filenames, 'r')  # r 代表read
    line = f.readline()  # 以行的形式进行读取文件
    print('line',line)
    list1 = []
    while line:
        a = line.split()
        str = a[0]   # 读取该行的第一个数据
        if str == '2':   #可修改标签
            list1.append(filenames)
            # shutil.copy(filenames, './labels0')
            shutil.copy(filenames, '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/chair/labels')
            print("匹配到:" + filenames)
        line = f.readline()
    f.close()

if __name__ == "__main__":
    # filenames = './label10'  #标签根目录
    filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/final_project/dataset/test/coco-20/images/train/labels'  #标签根目录
    arr = []
    eachFile(filenames)

# Classes
# nc: 20  # number of classes
# names: ['bus', 'suitcase','chair','dining table','bicycle','toilet','car', 'sink','boat','airplane','truck','tv','couch','refrigerator','oven','motorcycle','train','bed','microwave','toaster']  

# # 写入目标文件中，格式为 id x y w h
# out_file.write(str(cls_id) + " " + " ".join(str(x) for x in bbox) + '\n')

# -*- coding: utf-8 -*-

# import os
# import shutil

# root_dir = "../一级目录/"  #根目录
# txt_dir = root_dir + "labels/"  #标签
# image_dir = root_dir + "images/"  #结果存放地址
# origin_image_dir = "../images"  #原始图片地址
# count = 0
# # train_data_list = os.listdir(images_dir)
# for file in os.listdir(txt_dir):
#     # count += 1
#     # if file not in train_data_list:
#     print(count, " ", file)
#     shutil.copy(origin_image_dir + '/' + os.path.splitext(file)[0] + '.jpg',
#                 image_dir + '/' + os.path.splitext(file)[0] + '.jpg')
